from rest_framework.viewsets import ModelViewSet
from meiduo_admin.utils import PageNum
from meiduo_admin.serializers.skus import SKUSerializer
from goods.models import SKU


class SKUViewSet(ModelViewSet):
    pagination_class = PageNum
    # queryset = SKU.objects.all()
    serializer_class = SKUSerializer

    # 根据不同的请求方式返回不同序列化器
    def get_queryset(self):
        # 提取keyword
        keyword = self.request.query_params.get('keyword')

        if keyword == '' or keyword is None:
            return SKU.objects.all()
        else:
            return SKU.objects.filter(id=keyword)
